package com.linyaonan.leetcode.medium._341;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NestedIterator implements Iterator<Integer> {

    public static ArrayList<Integer> list = new ArrayList<>();

    public static int index = 0;

    public NestedIterator(List<NestedInteger> nestedList) {
        saveData(nestedList);
    }

    private static void saveData(List<NestedInteger> nestedList) {
        for (NestedInteger nestedInteger : nestedList) {
            if (nestedInteger.isInteger()) {
                list.add(nestedInteger.getInteger());
            } else {
                // 递归压入队列
                saveData(nestedInteger.getList());
            }
        }
    }

    @Override
    public Integer next() {
        if (hasNext()) {
            return list.get(index++);
        } else {
            return null;
        }
    }

    @Override
    public boolean hasNext() {
        return index < list.size() - 1;
    }
}